package de.dhbw.vs.permachat.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.googlecode.mgwt.mvp.client.AnimatableDisplay;
import com.googlecode.mgwt.mvp.client.AnimatingActivityManager;
import com.googlecode.mgwt.mvp.client.history.MGWTPlaceHistoryHandler;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.MGWTSettings;
import com.googlecode.mgwt.ui.client.MGWTSettings.ViewPort;
import com.googlecode.mgwt.ui.client.MGWTSettings.ViewPort.DENSITY;

import de.dhbw.vs.permachat.client.activities.login.LoginPlace;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class PermaChat implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		//Fix for java.util.Date transfer between Jackson (server)
		//and RestyGwt
		org.fusesource.restygwt.client.Defaults.setDateFormat(null);
		
	    GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

	        @Override
	        public void onUncaughtException(Throwable e) {
	          Window.alert("uncaught: " + e.getMessage());
	          e.printStackTrace();

	        }
	      });

	      new Timer() {

	        @Override
	        public void run() {
	          start();

	        }
	      }.schedule(1);
	}
	
	private void start() {

	    // MGWTColorScheme.setBaseColor("#56a60D");
	    // MGWTColorScheme.setFontColor("#eee");
	    //
	    // MGWTStyle.setTheme(new MGWTColorTheme());
	    //
	    // MGWTStyle.setDefaultBundle((MGWTClientBundle)
	    // GWT.create(MGWTStandardBundle.class));
	    // MGWTStyle.getDefaultClientBundle().getMainCss().ensureInjected();

	    // MGWTStyle.setTheme(new CustomTheme());

	    /*SuperDevModeUtil.showDevMode();*/

	    ViewPort viewPort = new MGWTSettings.ViewPort();
	    viewPort.setTargetDensity(DENSITY.MEDIUM);
	    viewPort.setUserScaleAble(false).setMinimumScale(1.0).setMinimumScale(1.0).setMaximumScale(1.0);

	    MGWTSettings settings = new MGWTSettings();
	    settings.setViewPort(viewPort);
	    settings.setIconUrl("logo.png");
	    settings.setAddGlosToIcon(true);
	    settings.setFullscreen(true);
	    settings.setPreventScrolling(true);

	    MGWT.applySettings(settings);

	    final ClientFactory clientFactory = new ClientFactoryImpl();

	    // Start PlaceHistoryHandler with our PlaceHistoryMapper
	    AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);


	    createPhoneDisplay(clientFactory);

	    AppHistoryObserver historyObserver = new AppHistoryObserver();

	    MGWTPlaceHistoryHandler historyHandler = new MGWTPlaceHistoryHandler(historyMapper, historyObserver);

	    historyHandler.register(clientFactory.getPlaceController(), clientFactory.getEventBus(), new LoginPlace());
	    historyHandler.handleCurrentHistory();

	  }
	
	private void createPhoneDisplay(ClientFactory clientFactory) {
	    AnimatableDisplay display = GWT.create(AnimatableDisplay.class);

	    PhoneActivityMapper appActivityMapper = new PhoneActivityMapper(clientFactory);

	    PhoneAnimationMapper appAnimationMapper = new PhoneAnimationMapper();

	    AnimatingActivityManager activityManager = new AnimatingActivityManager(appActivityMapper, appAnimationMapper, clientFactory.getEventBus());

	    activityManager.setDisplay(display);

	    RootPanel.get().add(display);

	  }
}
